import * as nodeFileSys from "fs";
import * as nodePath from "path";
import { ROOT_DIR } from "./global-helper";
import { Pin1_Yin1_Item } from ".";
import { getStringCharFrom } from "./binary-helper";

export async function writeBufAsStringByGroupPinYin(
  bufU16arr: Uint16Array,
  start: number,
  maxLength = 100
) {
  let resultList: Array<Array<number>> = [
    []
  ];
  let resultItemIndex = 0;
  for (let i = 0; i < maxLength; i++) {

  }
}
export async function writeBufAsString(
  bufU16arr: Uint16Array,
  start: number,
  maxLength = 100
) {
  const numList = Array.from(bufU16arr.slice(start, start + maxLength));
  const numArray: Array<Array<number>> = [];
  numList.forEach((item, index) => {
    let numIndex = Math.floor(index / 5);
    if (numArray[numIndex]) {
      numArray[numIndex].push(item);
    } else {
      numArray[numIndex] = [
        item
      ];
    }
  });
  let content = numArray.map((arrayItem) => {
    return arrayItem.join(" ");
  }).join("\n");
  return nodeFileSys.promises.writeFile(
    nodePath.join(ROOT_DIR, "./result/buf-part-string.txt"),
    content,
    "utf8"
  );
}

export async function writePinYinResult(arr: Array<Pin1_Yin1_Item>) {
  return nodeFileSys.promises.writeFile(
    nodePath.join(ROOT_DIR, "./result/pin-yin-list.json"),
    JSON.stringify(arr, null, 2),
    "utf8"
  );
}

export async function writeHanZiBufText(hanZiBuf: Uint16Array) {
  let arr: string[] = [];
  for (let i = 0; i < hanZiBuf.length; i += 2) {
    const charCode = getStringCharFrom(
      hanZiBuf[i],
      hanZiBuf[i + 1],
    );
    if (i / 2 < 10) {
      console.log("charCode", charCode)
    }
  }
  // await nodeFileSys.promises.writeFile(
  //   nodePath.join(ROOT_DIR, "./result/parse-han-zi.txt"),
  //   arr.slice(500).join("\n"),
  //   "utf8"
  // );
}


export async function writeLastResult(arr: Array<any>) {
  await nodeFileSys.promises.writeFile(
    nodePath.join(ROOT_DIR, "./result/last-result.json"),
    JSON.stringify(arr, null, 2),
    "utf8"
  );
}